<?php
  // configuration
  require_once("../includes/config.php"); 
  
  //if the request comes through GET Request
  if ($_SERVER["REQUEST_METHOD"] == "GET"&&$_SESSION["r"]<=1)  
  {
    
    if($_GET['filter'] == 1)
    {
      //Check if has contracts related
      $query = query('SELECT * FROM contract_apartments WHERE apartment_id =?',$_GET['id']);
      if(empty($query))
      {
        $sql = query('DELETE FROM apartments WHERE id =?',$_GET['id']);
        $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      }
      else
      {
        $_SESSION['error_message'] = "لا يمكن حذف الوحدة لوجود حجوزات مرتبطة بها"; 
      }
      redirect("apartments.php");
		die();
    }
    
    if($_GET['filter'] == 2)
    {
      $results = query('SELECT * FROM contracts WHERE client_id =?',$_GET['id']);
      if(!empty($results))
        $_SESSION['error_message'] = "لا يمكن حذف العميل لإرتباطه بعقود إيجار"; 
      else
      {
        $sql = query('DELETE FROM clients WHERE id =?',$_GET['id']);
        $_SESSION['success_message'] = "تم الحذف بنجاح";
      }      
      redirect("customers.php");    
	  die();
    }
    
    if($_GET['filter'] == 3)
    {
      $sql = query('DELETE FROM furnitures WHERE furnitures_id =?',$_GET['id']);
      
      $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      redirect("furnitures.php");    
	  die();
    }
    
    if($_GET['filter'] == 4)
    {
      
      $results = query('SELECT * FROM contracts WHERE company_id =?',$_GET['id']);
      if(!empty($results))
         $_SESSION['error_message'] = "لا يمكن حذف الشركة لإرتباطها بحجوزات قديمة أو جديدة"; 
      else
      {
        $sql = query('DELETE FROM companies WHERE company_id =?',$_GET['id']);
        $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      }
      redirect("customers.php?check=1");   
	die();	  
    }
	
	if($_GET['filter'] == 5)
    {
      $sql = query('DELETE FROM payment_vouchers WHERE payment_id =?',$_GET['id']);
      
      $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      redirect("payment_vouchers.php");   
	die();	  
    }
	
	if($_GET['filter'] == 6)
    {
      $sql = query('DELETE FROM sub_services WHERE sub_serv_id  =?',$_GET['id']);
      
      $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      redirect("sub_services-settings.php?id=".$_GET['other']);   
	die();	  
    }
	
	if($_GET['filter'] == 7)
    {
      $sql = query('DELETE FROM services WHERE service_id  =?',$_GET['id']);
      
      $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      redirect("services-settings.php");    
	  die();
    }
	
	if($_GET['filter'] == 8)
    {
      $sql = query('DELETE FROM unit_type WHERE type_id =?',$_GET['id']);
      
      $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      redirect("apartments_type.php");    
	  die();
    }
	if($_GET['filter'] == 9)
    {
      $sql = query('DELETE FROM recipt_vouchers WHERE recipet_id =?',$_GET['id']);
      
      $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      redirect("recipt_vouchers.php");   
		die();
    }
	
	if($_GET['filter'] == 10)
    {
      $sql = query('DELETE FROM service_vouchers WHERE payment_id =?',$_GET['id']);
      
      $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      redirect("service_vouchers.php");  
		die();	  
    }
	
	if($_GET['filter'] == 11)
    { 
	$contracts = query('SELECT * FROM contracts WHERE contract_id =? ',$_GET['id']);
	$payments = query('SELECT * FROM payment_vouchers WHERE contract_id =? ',$_GET['id']);
	$recipts = query('SELECT * FROM recipt_vouchers WHERE contract_id =? ',$_GET['id']);
	$services = query('SELECT * FROM service_vouchers WHERE contract_id =? ',$_GET['id']);
	
	if($contracts[0]['contract_status']==1 && empty($payments) && empty($recipts) &&  empty($services)) {
      $sql = query('DELETE FROM contracts WHERE contract_id =? ',$_GET['id']);
      
      //GET ALL contract Apartments
      $apartments = query("SELECT * FROM contract_apartments WHERE contract_id = ?",$_GET['id']);
	  
	  //SELECTING appropriate contract for each apartment if any
	  foreach($apartments as $apartment)
	  {
		$in_contracts = query("SELECT * FROM contract_apartments LEFT JOIN contracts ON 
		contract_apartments.contract_id = contracts.contract_id 
		WHERE apartment_id = ? and contract_status = 2",$apartment['apartment_id']);
		
		
		//if the chosen apartment has a contract cover this period
		if(!empty($in_contracts))
		{
			$update= query('UPDATE apartments SET status = ? WHERE id=?',1,$apartment['apartment_id']);
		}
		else
		{
		  //check for pre-booked contracts ordered BY apartment entrance date
		  $booked_contracts = query("SELECT * FROM contract_apartments 
		  LEFT JOIN contracts ON 
			contract_apartments.contract_id = contracts.contract_id 
		  WHERE apartment_id = ? and contract_status = 1",$apartment['apartment_id']);
		  
		  if(!empty($booked_contracts))
		  {
			$update= query('UPDATE apartments SET status = ? WHERE id=?',6,$apartment['apartment_id']);
		  }
		  else
		  {
			$update= query('UPDATE apartments SET status = ? WHERE id=?',2,$apartment['apartment_id']);
		  }
		}
	  }
      $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      redirect("bookings.php"); 
     } else {
     $_SESSION['error_message']= "لا يمكن حذف العقد لوجود سندات مرتبطة به";
	  redirect("bookings.php"); 
      }
	die();	  
    }
	
	if($_GET['filter'] == 12)
    {
		if ($_GET['rtype']>1)
		{
		  $sql = query('DELETE FROM apartments_owner WHERE user_id =?',$_GET['id']);
		}
		else
		{
			$sql = query('DELETE FROM apartments_owner WHERE id =?',$_GET['id']);
			$sql = query('DELETE FROM clients WHERE owner_id =?',$_GET['id']);
			$sql = query('DELETE FROM companies WHERE owner_id =?',$_GET['id']);
			$sql = query('DELETE FROM conditions WHERE owner_id =?',$_GET['id']);
			$sql = query('DELETE FROM contracts WHERE owner_id =?',$_GET['id']);
			$sql = query('DELETE FROM payment_vouchers WHERE owner_id =?',$_GET['id']);
			$sql = query('DELETE FROM periods WHERE owner_id =?',$_GET['id']);
			$sql = query('DELETE FROM recipt_vouchers WHERE owner_id =?',$_GET['id']);
			$sql = query('DELETE FROM service_vouchers WHERE owner_id =?',$_GET['id']);
			$sql=query("SELECT * FROM `apartments` WHERE owner_id =?",$_GET['id']);
			if (!empty($sql))
			{
				$sql=$sql[0];
				foreach ($sql as $info1)
				{
				$sql = query('DELETE FROM furnitures WHERE apartments_id =?',$info1['id']);
				}
			}
			$sql = query('DELETE FROM apartments WHERE owner_id =?',$_GET['id']);
			
			$sql=query("SELECT * FROM `services` WHERE owner_id=?",$_GET['id']);
			if (!empty($sql))
			{
				$sql=$sql[0];
				foreach ($sql as $info2)
				{
				$sql = query('DELETE FROM `sub_services` WHERE service_id =?',$info2['service_id']);
				}
			}
			$sql = query('DELETE FROM services WHERE owner_id =?',$_GET['id']);
			$sql = query('DELETE FROM unit_type WHERE owner_id =?',$_GET['id']);	  
		}
      $_SESSION['success_message'] = "تم الحذف بنجاح"; 
      redirect("usersManage.php");    
		die();
    }
	
  }
  else
  {
    apologize("Sorry!");
  }
 
?>